package com.aiti.lulian.controller;


import cn.hutool.db.meta.Table;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.ProductInfoDto;
import com.aiti.lulian.entity.Product;
import com.aiti.lulian.service.IProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController extends WebController {

    private final IProductService productService;

    public ProductController(IProductService productService) {
        this.productService = productService;
    }

    /**
     * 添加商品
     * @param dto 商品参数
     * @return
     */
    @PostMapping("/addProduct")
    public Message addProduct(@RequestBody ProductInfoDto dto){
        String userId = findUserId();
        try{
            productService.addProduct(dto,userId);
        }catch (Exception e){
            return Message.fail(e.getMessage());
        }
        return Message.success();
    }

    @DeleteMapping("/deleteProductById")
    public Message deleteProductById(@RequestBody ProductInfoDto dto){
        if(StringUtils.isBlank(dto.getBaseId())){
            return Message.fail("没有找到Id");
        }
        productService.removeById(dto.getBaseId());
        return Message.success();
    }

    @GetMapping("queryProductList")
    public Message queryProductList(ProductInfoDto dto){

        String userId = findUserId();

        Integer pageSize = dto.getPageSize();
        Integer pageNum = dto.getPageNum();
        Page<Product> page = startPage(pageNum,pageSize);

        return Message.success(productService.queryProductList(page,dto,userId));
    }

    @GetMapping("/showProductById")
    public Message showProductById(ProductInfoDto dto){
        return productService.showProductById(dto);
    }

    @PutMapping("/updateProductById")
    public Message updateProductById(@RequestBody ProductInfoDto dto){
        String userId = findUserId();
        productService.updateProductById(dto,userId);
        return Message.success();
    }

    @GetMapping("/queryProductListByEnterpriseId")
    public Message queryProductListByEnterpriseId(@RequestParam("enterpriseId") String enterpriseId){
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<Product>();
        lqw.eq(Product::getEnterpriseId,enterpriseId).eq(Product::getIsDelete,"0");
        lqw.select(Product::getBaseId,Product::getBaseName);
        List<Product> list = productService.list(lqw);
        return Message.success(list);
    }

    //小程序端获取根据企业id获取商品列表
    @GetMapping("/queryProductListByEnterpriseIdForApp")
    public Message queryProductListByEnterpriseIdForApp(String enterpriseId){
        return Message.success(productService.queryProductListByEnterpriseIdForApp(enterpriseId));
    }

    @PutMapping("/update")
    public Message update(@RequestBody ProductInfoDto dto){
        String userId = findUserId();
        productService.updateProductInfo(dto,userId);
        return Message.success();
    }

}
